package app.controller.msgmanage;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import app.utils.DataResponse;

/**
 * 这是一个用于处理转发服务器发送来的消息的控制器，是平台数据的二级入口
 * @author 昊琦
 * @since 2018/6/12
 */
@Controller
public class MsgManage {
	
	/**
	 * 这是一个用于处理转发服务器发送来的消息的控制器，是平台数据的二级入口
	 * @param request
	 * @param response
	 */
	@RequestMapping(value= {"/msgmanage","/msgmanage.do"}, method =RequestMethod.POST)
	public void msgManage(HttpServletRequest request,HttpServletResponse response){
		
		String reponseMsg ="";
		
		//用于接收主服务器发送来的消息
		String mess = request.getHeader("mess");
		//用于接收主服务器发送来的消息的时间
		String timeNow = request.getHeader("d_time");
		
		long d_time = 0L;
		
		if (timeNow!=null&&!timeNow.replace(" ", "").equals("")&&!timeNow.replace(" ", "").equals("null")) {
			d_time = Long.valueOf(timeNow);
		}
		//判断消息的有效性
		if (mess==null||mess.equals("")) {
			
			System.out.println("没有收到有效数据");
			reponseMsg="0";

			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			return;
		}
		
		//正则表达式校验
		if (devCodeCheck(mess)==false) {
			System.out.println("正则表达式匹配不通过");
			reponseMsg="0";
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			return;
		}
		
		
		//去括号
		mess = mess.substring(1, mess.length()-1);
		//按逗号做分割
		String[] msgArray = mess.split(",");
		
		
		//根据消息中的控制类型码做相应处理
		switch (msgArray[1]) {
		//硬件主动上报的故障信息
		case "98":

			reponseMsg =new MsgManageSupport().faultHandle(msgArray,d_time); 
			
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件响应服务器读取心跳参数
		case "51":
			
			new MsgManageSupport().heartbeatHandle(msgArray,d_time);
			reponseMsg="0";
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件响应服务器设置心跳参数
		case "53":
			
			new MsgManageSupport().deviceRespondToChangeHeartbeat(msgArray,d_time);
			reponseMsg="0";
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件响应服务器读取等待用户开锁时间参数
		case "55":
			
			new MsgManageSupport().deviceRespondToServerRequestLockTime(msgArray, d_time);
			
			reponseMsg="0";
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件响应服务器设置等待用户开锁时间参数
		case "57":
			
			new MsgManageSupport().deviceRespondToServerSetLockTime(msgArray, d_time);
			
			reponseMsg="0";
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件响应服务器读取等待用户开门时间参数
		case "59":
			
			new MsgManageSupport().deviceRespondToServerRequestDoorTime(msgArray, d_time);
			
			reponseMsg="0";
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件响应服务器设置等待用户开门时间参数
		case "61":

			new MsgManageSupport().deviceRespondToServerSetDoorTime(msgArray, d_time);
			
			reponseMsg="0";
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件响应服务器设置服务器地址及端口
		case "63":
			new MsgManageSupport().deviceRespondToChangeIp(msgArray, d_time);
			reponseMsg="0";
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件向备用服务器主动报告与主服务器通信异常
		case "64":
			reponseMsg=new MsgManageSupport().deviceReportCommunicationsError(msgArray, d_time);
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件主动上报心跳
		case "00":
			
			reponseMsg = new MsgManageSupport().receiveHeartbeat(msgArray, d_time);
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//地井硬件主动上报地井井盖状态变化信息
//		case "04":
//
//			reponseMsg = new MsgManageSupport().coverReportStu(msgArray, d_time);
//			return "data";
		//通断电设备主动上报通断电状态信息，地井主动上报井盖状态变化信息
		case "06":
			
			reponseMsg =new MsgManageSupport().onoffDeviceReportStu(msgArray, d_time);
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件响应完成机房开门
		case "09":
			
			new MsgManageSupport().deviceRespondToFinishOpenDoor(msgArray, d_time);
			reponseMsg ="0";
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//机柜锁硬件主动上报电量信息
		case "10":
			
			reponseMsg = new MsgManageSupport().lockReportElectricQuantity(msgArray, d_time);
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件主动上报机柜锁按键触发开门请求
		case "12":

			reponseMsg = new MsgManageSupport().lockDeviceRequestOpenDoor(msgArray, d_time);
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//硬件主动上报机柜锁和门状态发生变化
		case "14":
		
			reponseMsg = new MsgManageSupport().lockDeviceReportLockAndDoorStu(msgArray, d_time);
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		//用户未执行开门，硬件主动上报
		case "16":
			
			reponseMsg = new MsgManageSupport().deviceReportUserNotOpenDoor(msgArray, d_time);
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		
		default:
			reponseMsg="0";
			//返回数据
			DataResponse.jsonResponse(response, reponseMsg);
			break;
		}
		

		
	}
	
	
	/**
	 * 这个方法被用于校验接收到的硬件上行消息数据格式是否是包含[设备编码,控制类型码] 格式的
	 * @param devCode
	 * @return
	 */
	private Boolean devCodeCheck(String devCode){
		
		String reg = "^\\[[A-Z]{1}[0-9]{5},\\d\\d\\S*\\]$";
		
		Pattern pattern = Pattern.compile(reg);
		
		Matcher matcher = pattern.matcher(devCode);
		return matcher.matches();
	}
}
